package cn.com.blueInfo.bpm.system.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.system.entity.RoleMenu;
import cn.com.blueInfo.bpm.system.enums.RoleMenuVisibleFlag;
import cn.com.blueInfo.bpm.system.mapper.RoleMenuMapper;
import cn.com.blueInfo.bpm.system.repository.RoleMenuRepository;
import cn.com.blueInfo.bpm.system.request.RoleMenuDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 角色-菜单关联表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class RoleMenuRepositoryImpl
        extends ServiceImpl<RoleMenuMapper, RoleMenu>
        implements RoleMenuRepository, BaseRepository<RoleMenu, RoleMenuDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增角色-菜单关联
     * @param roleMenu 角色-菜单关联参数
     */
    @Override
    public Integer createRoleMenu(RoleMenu roleMenu) {
        return sqlExecuteResult(save(roleMenu));
    }

    /**
     * 批量新增角色-菜单关联
     * @param roleMenuList 角色-菜单关联参数列表
     */
    @Override
    public Integer batchCreateRoleMenu(List<RoleMenu> roleMenuList) {
        return sqlExecuteResult(saveBatch(roleMenuList), roleMenuList.size());
    }

    /**
     * 删除角色-菜单关联
     * @param roleMenuId 角色-菜单关联ID
     */
    @Override
    public Integer deleteRoleMenu(Long roleMenuId) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setId(roleMenuId);
        roleMenu.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(roleMenu));
    }

    /**
     * 批量删除角色-菜单关联
     * @param roleMenuIdList 角色-菜单关联ID列表
     */
    @Override
    public Integer batchDeleteRoleMenu(List<Long> roleMenuIdList) {
        List<RoleMenu> roleMenuList = new ArrayList<>();
        for (Long roleMenuId : roleMenuIdList) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setId(roleMenuId);
            roleMenu.setDeleted(BaseDeleted.DELETED);
            roleMenuList.add(roleMenu);
        }
        return sqlExecuteResult(removeBatchByIds(roleMenuList), roleMenuList.size());
    }

    /**
     * 更新部分角色-菜单关联
     * @param roleMenu 角色-菜单关联参数
     */
    @Override
    public Integer updateRoleMenuPartial(RoleMenu roleMenu) {
        validateUpdateId(roleMenu.getId());
        LambdaUpdateWrapper<RoleMenu> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoleMenu::getId, roleMenu.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部角色-菜单关联
     * @param roleMenu 角色-菜单关联参数
     */
    @Override
    public Integer updateRoleMenu(RoleMenu roleMenu) {
        validateUpdateId(roleMenu.getId());
        return sqlExecuteResult(updateById(roleMenu));
    }

    /**
     * 查询一条角色-菜单关联
     * @param roleMenuId 角色-菜单关联ID
     */
    @Override
    public RoleMenu getRoleMenuById(Long roleMenuId) {
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getId, roleMenuId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询角色-菜单关联
     * @param roleMenuQueryDTO 角色-菜单关联参数
     */
    @Override
    public Page<RoleMenu> getRoleMenuPage(RoleMenuDTO.QueryDTO roleMenuQueryDTO) {
        Page<RoleMenu> roleMenuPage = new Page<>(
                roleMenuQueryDTO.getPage(), roleMenuQueryDTO.getSize());
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, roleMenuQueryDTO);

        BaseDeleted deleted = roleMenuQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(roleMenuPage, queryWrapper);
        } else {
            return page(roleMenuPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 角色-菜单关联 数量
     * @param roleMenuQueryDTO 角色-菜单关联参数
     */
    @Override
    public Long getRoleMenuCount(RoleMenuDTO.QueryDTO roleMenuQueryDTO) {
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, roleMenuQueryDTO);

        BaseDeleted deleted = roleMenuQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 角色-菜单关联
     * @param roleMenuQueryDTO 角色-菜单关联参数
     */
    @Override
    public List<RoleMenu> getRoleMenuList(RoleMenuDTO.QueryDTO roleMenuQueryDTO) {
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, roleMenuQueryDTO);

        BaseDeleted deleted = roleMenuQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<RoleMenu> queryWrapper, RoleMenuDTO.QueryDTO roleMenuQueryDTO) {

        Long roleId = roleMenuQueryDTO.getRoleId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(roleId), RoleMenu::getRoleId, roleId);

        Long menuId = roleMenuQueryDTO.getMenuId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(menuId), RoleMenu::getMenuId, menuId);

        RoleMenuVisibleFlag visibleFlag = roleMenuQueryDTO.getVisibleFlag();
        queryWrapper.eq(ObjectUtils.isNotEmpty(visibleFlag), RoleMenu::getVisibleFlag, visibleFlag);

        addQueryWrapper(queryWrapper, roleMenuQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<RoleMenu, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                RoleMenu::getOrderNumber,
                RoleMenu::getTenantId,
                RoleMenu::getApplicationId,
                RoleMenu::getStatus,
                RoleMenu::getDeleted,
                RoleMenu::getDeleteUserId,
                RoleMenu::getDeleteTime,
                RoleMenu::getRemark,
                RoleMenu::getCreateUserId,
                RoleMenu::getCreateTime,
                RoleMenu::getUpdateUserId,
                RoleMenu::getUpdateTime,
                RoleMenu::getExtend1,
                RoleMenu::getExtend2,
                RoleMenu::getExtend3);
    }

}
